package com.scan.record.controller;

import java.io.IOException;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.scan.record.domain.ScanRecordInfoStat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.scan.record.domain.ScanRecordInfo;
import com.scan.record.service.IScanRecordInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 扫码信息记录Controller
 * 
 * @author ysz
 * @date 2023-04-22
 */
@RestController
@RequestMapping("/scan/record")
public class ScanRecordInfoController extends BaseController
{
    @Autowired
    private IScanRecordInfoService scanRecordInfoService;

    /**
     * 查询扫码信息记录列表
     */
//    @PreAuthorize("@ss.hasPermi('scan:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScanRecordInfo scanRecordInfo)
    {
//        startPage();
        List<ScanRecordInfo> list = scanRecordInfoService.selectScanRecordInfoList(scanRecordInfo);
        return getDataTable(list);
    }

    /**
     * 查询扫码信息记录列表-聚合
     */
//    @PreAuthorize("@ss.hasPermi('scan:record:list')")
    @GetMapping("/stat/{dateCycle}")
    public TableDataInfo stat(@PathVariable("dateCycle") String dateCycle)
    {
//        startPage();
        List<ScanRecordInfoStat> list = scanRecordInfoService.selectScanRecordInfoListStat(dateCycle);
        return getDataTable(list);
    }

    /**
     * 导出扫码信息记录列表
     */
    @PreAuthorize("@ss.hasPermi('scan:record:export')")
    @Log(title = "扫码信息记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScanRecordInfo scanRecordInfo)
    {
        List<ScanRecordInfo> list = scanRecordInfoService.selectScanRecordInfoList(scanRecordInfo);
        ExcelUtil<ScanRecordInfo> util = new ExcelUtil<ScanRecordInfo>(ScanRecordInfo.class);
        util.exportExcel(response, list, "扫码信息记录数据");
    }

//    /**
//     * 获取扫码信息记录详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('scan:record:query')")
//    @GetMapping(value = "/{scanId}")
//    public AjaxResult getInfo(@PathVariable("scanId") String scanId)
//    {
//        return success(scanRecordInfoService.selectScanRecordInfoByScanId(scanId));
//    }

    /**
     * 新增扫码信息记录
     */
//    @PreAuthorize("@ss.hasPermi('scan:record:add')")
    @Log(title = "扫码信息记录", businessType = BusinessType.INSERT)
    @GetMapping(value = "/add/{scanBelong}")
    public void add(@PathVariable("scanBelong") String scanBelong, HttpServletResponse response) throws IOException {
//        scanRecordInfoService.insertScanRecordInfo(scanBelong);
        scanRecordInfoService.insertScanRecordInfo(scanBelong);
        ServletOutputStream outputStream = null;
        try {
            final Path path = Path.of(RuoYiConfig.getProfile() +"/success.jpg");
            byte[] bytes = null;
            if (path.toFile().exists()) {
                try {
                    bytes = Files.readAllBytes(path);
                } catch (Exception e) {
                }
            }
            response.setContentType("image/jpeg");
            response.setCharacterEncoding("utf-8");
            outputStream = response.getOutputStream();
            outputStream.write(bytes);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
            if (outputStream != null) {
                outputStream.close();
            }
        }
//        return "ok";
    }

    /**
     * 修改扫码信息记录
     */
    @PreAuthorize("@ss.hasPermi('scan:record:edit')")
    @Log(title = "扫码信息记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScanRecordInfo scanRecordInfo)
    {
        return toAjax(scanRecordInfoService.updateScanRecordInfo(scanRecordInfo));
    }

    /**
     * 删除扫码信息记录
     */
    @PreAuthorize("@ss.hasPermi('scan:record:remove')")
    @Log(title = "扫码信息记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{scanIds}")
    public AjaxResult remove(@PathVariable String[] scanIds)
    {
        return toAjax(scanRecordInfoService.deleteScanRecordInfoByScanIds(scanIds));
    }
}
